package com.lcz.authority.common.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.lcz.authority.common.annotation.Excel;
import com.lcz.authority.common.domain.BaseEntity;
import com.zhou.starter.jpacomment.annotation.ColumnComment;
import com.zhou.starter.jpacomment.annotation.TableComment;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Set;


/**
 * @author lcz
 * @desc 角色表
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_role")
@Table(name = "sys_role")
@Entity
@TableComment("角色信息表")
public class SysRoleEntity extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 角色ID
     */
    @Id
    @TableId(type = IdType.ASSIGN_ID)
    @ColumnComment("主键id")
    private String roleId;

    /**
     * 角色名称
     */
    @NotBlank(message = "角色名称不能为空")
    @Size(max = 30, message = "角色名称长度不能超过30个字符")
    @Excel(name = "角色名称")
    @ColumnComment("角色名称")
    private String roleName;

    /**
     * 角色权限
     */
    @NotBlank(message = "权限字符不能为空")
    @Size(max = 100, message = "权限字符长度不能超过100个字符")
    @Excel(name = "权限字符")
    @ColumnComment("权限字符")
    private String roleKey;

    /**
     * 角色排序
     */
    @NotBlank(message = "显示顺序不能为空")
    @ColumnComment("角色排序")
    private String roleSort;


    /**
     * 菜单树选择项是否关联显示（ 0：父子不互相关联显示 1：父子互相关联显示）
     */
    @ColumnComment("菜单树选择项是否关联显示（ 0：父子不互相关联显示 1：父子互相关联显示）")
    private boolean menuCheckStrictly;


    /**
     * 角色状态（0正常 1停用）
     */
    @Excel(name = "角色状态", readConverterExp = "0=正常,1=停用")
    @ColumnComment("角色状态（0正常 1停用）")
    private String status;

    /**
     * 用户是否存在此角色标识 默认不存在
     */
    @TableField(exist = false)
    @Transient
    private boolean flag = false;

    /**
     * 菜单组
     */
    @TableField(exist = false)
    @Transient
    private String[] menuIds;

    /**
     * 角色菜单权限
     */
    @TableField(exist = false)
    @Transient
    private Set<String> permissions;

    /**
     * 备注
     */
    @ColumnComment("备注")
    private String remark;

    /**
     * 部门树选择项是否关联显示（0：父子不互相关联显示 1：父子互相关联显示 ）
     */
    @TableField(exist = false)
    @Transient
    private boolean deptCheckStrictly;

    public SysRoleEntity(String roleId) {
        this.roleId = roleId;
    }

    public static boolean isAdmin(String roleKey) {
        return "admin".equals(roleKey);
    }

    public boolean isAdmin() {
        return isAdmin(this.roleKey);
    }

    public static class IsStatus {
        /**
         * 0：正常
         */
        public static final int NO = 0;
        /**
         * 1：停用
         */
        public static final int YES = 1;
    }
}
